package com.zhenyi.tuangou.index;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.MediaApi;
import com.jfinal.weixin.sdk.api.MediaFile;
import com.utils.PathUtils;
import com.utils.image.UploadUtils;
import com.zhenyi.common.model.TgCommunity;
import com.zhenyi.common.model.TgUser;

public class CommunityController extends TGController {

	@Clear
	@Override
	public void submit() {
		super.submit();
		String text = getPara("text");
		String mediaIds = getPara("mediaIds");
		System.out.println(mediaIds);
		String[] ids = mediaIds.split(";");
		StringBuffer sb = new StringBuffer();
		for (String id : ids) {
			if (StrKit.isBlank(id)) {
				continue;
			}
			MediaFile mediaFile = MediaApi.getMedia(id);
			String fileName = mediaFile.getFileName() + ".jpg";
			File file = new File(PathUtils.getFile("tmp", fileName));
			BufferedInputStream stream = mediaFile.getFileStream();
			try {
				inputstreamtofile(stream, file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			String url = UploadUtils.upload(file);
			sb.append(url).append(";");
		}

		TgCommunity post = new TgCommunity();
		post.setContent(text);
		post.setImages(sb.toString());
		post.setUserId(getUser().getId());
		if (post.save()) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}

	public void inputstreamtofile(InputStream ins, File file) throws IOException {
		OutputStream os = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
			os.write(buffer, 0, bytesRead);
		}
		os.close();
		ins.close();
	}

	@Override
	public void index() {
		List<Record> list = Db.find(Db.getSql("findCommunity"));
		for (Record item : list) {
			if (StrKit.notBlank(item.getStr("likeLogo"))) {
				String[] logos = item.getStr("likeLogo").split(";");
				if (logos.length > 6) {
					String[] logoList = new String[7];
					System.arraycopy(logos, logos.length - 7, logoList, 0, 6);
					logoList[6] = "";
					item.set("logoList", logoList);
				} else {
					item.set("logoList", logos);
				}
			}
			if (StrKit.notBlank(item.getStr("images"))) {
				String[] imgs = item.getStr("images").split(";");
				item.set("imgList", imgs);
			}
		}
		setAttr("items", list);
		render("/res/community/index.html");
	}

	public void like() {
		Long postId = getParaToLong("postId");
		TgUser user = getUser();
		TgCommunity post = TgCommunity.dao.findById(postId);
		if (post == null) {
			renderFailure(404, "动态不存在");
			return;
		}
		if (StrKit.notBlank(post.getLikeLogo()) && post.getLikeLogo().contains(user.getLogo())) {
			// renderFailure(406, "你已经点过赞了");
			renderSuccess();
			return;
		}
		String logo = user.getLogo();
		if (StrKit.notBlank(logo)) {
			StringBuffer sb = new StringBuffer(user.getLogo());
			String likeLogos = "";
			if (StrKit.notBlank(post.getLikeLogo())) {
				likeLogos = post.getLikeLogo();
			}
			post.setLikeLogo(sb.append(";").append(likeLogos).toString());
			if (post.update()) {
				renderSuccess();
				return;
			} else {
				renderFailure();
				return;
			}
		}
		renderSuccess();

	}

	public void post() {
		render("/res/community/post.html");
	}
}
